﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.MicroKernel.Registration;

namespace YoProgramo.Infrastructure.Tools.Windsor
{
    public static class WindsorExtensions {
        public static BasedOnDescriptor FirstNonGenericCoreInterface(this ServiceDescriptor descriptor, string interfaceNamespace) {
            return descriptor.Select(delegate(Type type, Type baseType) {
                                                                            var interfaces = type.GetInterfaces()
                                                                                .Where(t => t.IsGenericType == false && t.Namespace.StartsWith(interfaceNamespace));

                                                                            if (interfaces.Count() > 0) {
                                                                                return new[] { interfaces.ElementAt(0) };
                                                                            }

                                                                            return null;
            });
        }
    }
}